<?php
	require(CORE_DIR.'/shop/controller/ctl.cart.php');
	class shop_cart extends ctl_cart
	{
		function shop_cart(&$system)
		{
			parent::ctl_cart($system);
			$this->objCollocAddon = $this->system->loadModel('plugins/collocate_goods/collocAddon');
		}

		function addColloToCart()
		{
			$aParams = $this->objCart->getParams($_POST['goods'],$gid, $pid, $stradj, $pmtid);
			if($aParams['pid'] == -1){
				$this->begin($_SERVER['HTTP_REFERER']);
				trigger_error(__('加入购物车失败：无此货品'),E_USER_ERROR);
				$this->end();
			}
			$_num = intval($aParams['num']);
			if($_num){
				$num = $_num;
			}else{
				$num = intval($num);
			}
			if(!$num) $num = 1;

			$status = $this->objCart->addToCart('g', $aParams, $num);
			if($status === 'notify'){
				$status = false;
				$this->begin($this->system->mkUrl("product","gnotify",array($gid, $pid)));
				$this->setError(10001);
				if($_POST['mini_cart']){
					header("HTTP/1.0 404 Not Found");
				}
				trigger_error(__('加入购物车失败：商品缺货，转入缺货登记'),E_USER_ERROR);
				$this->end();
			}elseif(!$status){
				$status = false;
				$this->begin($_SERVER['HTTP_REFERER']);
				$this->setError(10002);
				if($_POST['mini_cart']){
					header("HTTP/1.0 404 Not Found");
				}
				trigger_error(__('加入购物车失败: 商品库存不足或者提交参数错误！'),E_USER_ERROR);
				$this->end();
			}else{			
				//$objCollocAddon = $this->system->loadModel('plugins/collocate_goods/collocAddon');
				if ($aRule = $this->objCollocAddon->createRule($_POST['goods']))
				{
					$this->objCollocAddon->saveCollocCart($aRule);
				}

				if($_POST['fastbuy']){
                $this->checkout();
				}else{
					if($_POST['mini_cart']){
						$this->view(1);
						exit;
					}
					$this->redirect('cart');
				}
			}
		}

		function view($mini=0)
		{
			$sale = &$this->system->loadModel('trading/sale');
			$this->cart = $this->objCart->getCart('all');
			$trading = $sale->getCartObject($this->cart,$GLOBALS['runtime']['member_lv'],true);
			$acollocProduct = $this->objCollocAddon->getCollocProduct();
			$totalPrice = 0;
			foreach ($acollocProduct as $v)
			{
				$totalPrice += $v['totalPrice'];
			}
			$trading['totalPrice'] += $totalPrice;
			$this->pagedata['trading'] = &$trading;

			$aColloc = $this->objCollocAddon->splitAddon();
			$collocCount = 0;
			$collocNumber = 0;
			foreach ($aColloc as $k =>$v)
			{
				$collocCount += count(array_keys($v));
				$collocNumber += array_sum(array_values($v));
			}
			$this->pagedata['cartCount'] = $_COOKIE['CART_COUNT']+$collocCount;
			$this->pagedata['cartNumber'] = $_COOKIE['CART_NUMBER']+$collocNumber;
			$this->__tmpl = $mini?'cart/mini_cart.html':'cart/view.html';
			$this->output();
		}
		
		/* 该函数暂时弃用 */
		function updateCollocAddon()
		{
			//$_POST[coNum][colloc,95,317][267]=>2
			//$objCollocAddon = $this->system->loadModel('plugins/collocate_goods/collocAddon');
			if (!$this->objCollocAddon->updateCollocAddon($_POST['coNum']),$aMsg)
			{
				echo $aMsg;
			}
			else
			{
				$this->cartTotal();
			}
		}

		function removeCart()
		{
			$this->objCollocAddon->removeCollocAddon($_POST['cartNum']);
			$this->cartTotal();
		}

		function updateCart($objType='g', $key='')()
		{
			if ('co' == $objType)
			{
				//$objCollocAddon = $this->system->loadModel('plugins/collocate_goods/collocAddon');
				if (!$this->objCollocAddon->updateCollocAddon($_POST['coNum']),$aMsg)
				{
					echo $aMsg;
				}
				else
				{
					$this->cartTotal();
				}
			}
			else
			{
				$key = str_replace('@', '-', $key);
				$nQuantity = $_POST['cartNum'][$objType][$key];
				switch($objType) {
					case 'f':
						$oCart->member['member_lv_id'] =$GLOBALS['runtime']['member_lv'];
						$oCart->member['point'] = $this->member['point'];
						break;
					case 'g':
						break;
					case 'p':
						break;
					default:
						break;
				}
				if(!$this->objCart->updateCart($objType, $key, $nQuantity,$aError)){
					echo implode('',$aError);
				}else{
					$this->cartTotal();
				}
			}
		}

		function cartTotal()
		{
			$sale = &$this->system->loadModel('trading/sale');
			$trading = $sale->getCartObject($this->cart,$GLOBALS['runtime']['member_lv'],true);
			//$objCollocAddon = $this->system->loadModel('plugins/collocate_goods/collocAddon');
			$acollocProduct = $this->objCollocAddon->getCollocProduct();
			$totalPrice = 0;
			foreach ($acollocProduct as $v)
			{
				$totalPrice += $v['totalPrice'];
			}
			$trading['totalPrice'] += $totalPrice;
			$this->pagedata['trading'] = &$trading;
			$this->template_dir = CORE_DIR.'/shop/view/';
			$this->__tmpl = 'cart/cart_total.html';
			$this->output();
		}
	}

?>